package com.next.aappublicapp;

import java.io.UnsupportedEncodingException;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.next.aap.client.AssemblyConstituencyWithManifesto;
import com.next.aap.client.ManifestoDto;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.AapAsyncTask;

public class ManifestoActivity extends BaseActivity {

	public static final String ARGS_AC = "ARGS_AC";
	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";

	private LinearLayout loadinStatus;
	private TextView manifestoTitle;
	private WebView manifestoContent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manifesto);
		
		Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
		AssemblyConstituencyWithManifesto selectedAssemblyConstituencyWithManifesto = (AssemblyConstituencyWithManifesto)bundle.getSerializable(ManifestoActivity.ARGS_AC);
		
		loadinStatus = (LinearLayout)findViewById(R.id.loading_status);
		manifestoTitle = (TextView)findViewById(R.id.manifesto_title);
		manifestoContent = (WebView)findViewById(R.id.manifesto_content);
		
		manifestoTitle.setText("Manifesto for "+selectedAssemblyConstituencyWithManifesto.getName());
		loadManifestoAsync(selectedAssemblyConstituencyWithManifesto.getId());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.manifesto, menu);
		return true;
	}
	
	private void loadManifestoAsync(final long acId){
		
		new AapAsyncTask<String, String, ManifestoDto>("Manifesto","Get") {
			@Override
			protected void beforeExecute(){
				loadinStatus.setVisibility(View.VISIBLE);
				manifestoContent.setVisibility(View.GONE);
			}
			@Override
			protected ManifestoDto doInBackground(String... params) {
				ManifestoDto manifestoDto = null;
				try{
					manifestoDto = DataServices.getInstance().getAssemblyConstituencyManifesto(ManifestoActivity.this, acId);	
				}catch(Exception ex){
					Log.e("Ravi", "Eror while trying to get Manifesto", ex);
				}
				return manifestoDto;
			}
			
			@Override
			protected void afterExecute(ManifestoDto result){
				loadinStatus.setVisibility(View.GONE);
				manifestoContent.setVisibility(View.VISIBLE);
				String content =  "Manifesto Not Available at the moment";
				if(result != null){
					content = result.getContent();	
				}
				String base64;
				try {
					base64 = android.util.Base64.encodeToString(content.getBytes("UTF-8"), android.util.Base64.DEFAULT);
					manifestoContent.loadData(content, "text/html; charset=UTF-8", base64);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				
			}
		}.execute();
	}

}
